<?php
set_time_limit(0);
 $userDui=0;
if (isset($_POST['op']) && isset($_POST['yi'])){
    include '../c/config.php';
    $openid=trim($_POST['op']);
    $phone=trim($_POST['phone']);
    $mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
    $nianji=trim($_POST['nianji']);
    $getAn=array();
    if (3 == $nianji) {
       $getAn=json_decode(file_get_contents('../3.txt'), true);
    }
    if (4 == $nianji) {
       $getAn=json_decode(file_get_contents('../4.txt'), true);
    }
    if (5 == $nianji) {
       $getAn=json_decode(file_get_contents('../5.txt'), true);
    } 	 
	 if (isset($_POST['yi'])) {
	 	 $userYi=$_POST['yi'];
	 	 foreach ($userYi as $k => $v ){
	 	 	 if ($v == $getAn['yi'][$k]) {
	 	 	 	 $userDui++;
	 	 	 }
	 	 }
	 }
	 if (isset($_POST['er'])) {
	 	 $userEr=$_POST['er'];
	 	 foreach ($userEr as $k => $v ){
	 	 	 if ($v == $getAn['er'][$k]) {
	 	 	 	 $userDui++;
	 	 	 }
	 	 }
	 }
	 if (isset($_POST['san'])) {
	 	 $userSan=$_POST['san'];
	 	 foreach ($userSan as $k => $v ){
	 	 	 if ($v == $getAn['san'][$k]) {
	 	 	 	 $userDui++;
	 	 	 }
	 	 }
	 }
	$tableName='d3'; 
	if (3 == $nianji) {
		$tableName='d3';
	}
	if (4 == $nianji) {
		$tableName='d4';
	} 
	if (5 == $nianji) {
		$tableName='d5';
	}
    
    $query=$mysqli->query("SELECT COUNT(id) as count FROM `".$tableName."` WHERE `uid`='".$openid."' AND `phone` = '".$phone."'");
    $getData=$query->fetch_assoc();
    $count=$getData['count'];
    if ($count > 4) {
        	
    }else{
    	$mysqli->query("INSERT INTO `".$tableName."` (`id`, `uid`, `phone`,`dui`, `yi`,`er`,`san`,`createtime`) VALUES (NULL,'".$openid."','".$phone."','".$userDui."', '".json_encode($_POST['yi'], JSON_UNESCAPED_UNICODE)."','".json_encode($_POST['er'], JSON_UNESCAPED_UNICODE)."','".json_encode($_POST['san'], JSON_UNESCAPED_UNICODE)."', '".date("Y-m-d H:i:s")."')");
    }

    $mysqli->query("INSERT INTO `d_log` (`id`, `uid`,`nianji`, `phone`,`dui`, `yi`,`er`,`san`,`createtime`) VALUES (NULL,'".$openid."','".$nianji."','".$phone."','".$userDui."', '".json_encode($_POST['yi'], JSON_UNESCAPED_UNICODE)."','".json_encode($_POST['er'], JSON_UNESCAPED_UNICODE)."','".json_encode($_POST['san'], JSON_UNESCAPED_UNICODE)."', '".date("Y-m-d H:i:s")."')");

  
    $mysqli->close();

    echo json_encode(array('s'=>'y','d'=>$userDui));
}else{
    echo json_encode(array('s'=>'n','d'=>$userDui));
}

?>